package com.jim.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jim.dao.MiniBlogDAO;
import com.jim.to.MiniBlogTO;

public class BlogServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String content = (String) req.getParameter("content");
		MiniBlogDAO dao = new MiniBlogDAO();
		if (content != null) {
			MiniBlogTO blog = new MiniBlogTO();
			blog.setDate(new Date());
			blog.setContent(content);
			dao.saveMiniBlog(blog);
		}
		List blogs = dao.getMiniBlogList(0, 20);
		req.setAttribute("miniBlogs", blogs);
		this.getServletConfig().getServletContext().getRequestDispatcher(
				"/jsp/miniBlogList.jsp").forward(req, resp);
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

}
